<?php

class edit_judges
{
	var $mess = '';

	function edit_judges()
	{
		if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['itemID']))
		{
			$this->doAdd();
		}
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		$this->renderEdit();
		$this->renderAdd();
	}
	
	function doDelete()
	{
		global $skin_dir, $db, $lang;
		
		$qDel = "delete from ". JUDGES_TABLE . " Where ia_champ = " . $_POST['champID'] . " AND ia_user = " . $_POST['driverID'];
		$result = $db->sql_query($qDel);
		$this->mess = new addMessage(str_replace('{entry}', $lang['IncidentJudge'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields()) 
		{
			if(isset($_POST['allChamps']))
			{
				$champ = 0;
			}
			else
			{
				$champ = $_POST['itemID'];
			}
			
			$qUpd = "insert into  ". JUDGES_TABLE . " (ia_user, ia_champ) values(" . 
							$_POST['driverID'] . ", " . $champ . ")" ;
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', $lang['IncidentJudge'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields()
	{
		return is_numeric($_POST['itemID']) && is_numeric($_POST['driverID']);			  
	}
	
	function renderEdit()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$q = "Select pf_drivername, user_id, ia_champ From ". JUDGES_TABLE . ", " . DRIVERNAME_TABLE . " Where ia_user = user_id AND (ia_champ = 0 OR ia_champ = " . $_POST['itemID'] . ")
			  ORDER BY pf_drivername ASC";
		$result = $db->sql_query($q);
		
		$mess = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
		$mess->render();
		
		include $skin_dir . 'tpl/tableStart.tpl';
		
		while($row = $db->sql_fetchrow($result)) 
		{
			if($row['ia_champ'] == 0)
			{
				$row['pf_drivername'] .= ' (' . $lang['AdminAllChampionships'] . ')';
			}
			
			$champid = $_POST['itemID'];
			
			include $skin_dir . 'admin/editjudge.tpl';
		}
		
		include $skin_dir . 'tpl/tableEnd.tpl';
		
	}

	function renderAdd()
	{
		global $skin_dir, $db, $lang;
		
		$q = "Select n.pf_drivername, n.user_id FROM ".  DRIVERNAME_TABLE . " n, " . USERSGROUPS_TABLE . " g
			  Where g.user_id = n.user_id AND g.group_id = 3 AND g.user_id NOT IN 
			  		(SELECT ia_user FROM " . JUDGES_TABLE . " WHERE ia_champ = 0 OR ia_champ = " . $_POST['itemID'] . ")
			  ORDER BY pf_drivername ASC";
			  		
		$result = $db->sql_query($q);
			
		$driverOptions = '';
		while($row = $db->sql_fetchrow($result)) 
		{
			$driverOptions .= '<option value="' . $row['user_id'] . '">' . $row['pf_drivername'] . '</option>';
		}
		
		  
		$mess = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$mess->render();
		
		$champid = $_POST['itemID'];
		
		include $skin_dir . 'admin/addjudge.tpl';
	}
	
}
?>